package com.test;

import java.util.Random;

public class Wife implements Runnable {
    private Account account;
    public Wife(Account account) {
        this.account = account;
    }
    @Override
    public void run() {
        while (true) {
// 模拟每次取的钱
            int money = new Random().nextInt(1000);
//
            if (money > account.balance()) {
                Husband husband = new Husband();
                husband.notify();
                System.out.println("账户余额不足");
            } else {
                account.withdraw(money); // 取钱。
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
/**
 * 老公负责存钱。
 */
class Husband implements Runnable {
    private Account account;
    public Husband(Account account) {
        this.account = account;
    }

    public Husband() {

    }

    @Override
    public void run() {
        while (true) {
// 模拟每次存的钱
            int money = new Random().nextInt(1000);
            account.save(money);
            try {
                Thread.sleep(50);
                wait();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
